package com.cl.ks.controller;


import com.alibaba.fastjson.JSONObject;
import com.cl.ks.dto.KsDataTypeRequestModel;
import com.cl.ks.entity.KsDataField;
import com.cl.ks.entity.KsDataType;
import com.cl.ks.entity.SysSourceMappingTable;
import com.cl.ks.entity.SysSourceMappingTableField;
import com.cl.ks.service.KsDataFieldService;
import com.cl.ks.service.KsDataTypeService;
import com.cl.ks.service.SysSourceMappingTableFieldService;
import com.cl.ks.service.SysSourceMappingTableService;
import com.cl.ks.utils.CommonResult;
import com.cl.mvc.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("KsDataType")
public class KsDataTypeController extends BaseController<KsDataType> {
    private final KsDataTypeService ksDataTypeService;

    private final SysSourceMappingTableService sysSourceMappingTableService;
    private final SysSourceMappingTableFieldService sysSourceMappingTableFieldService;
    private final KsDataFieldService ksDataFieldService;

    public KsDataTypeController(KsDataTypeService service, SysSourceMappingTableService sysSourceMappingTableService, SysSourceMappingTableFieldService sysSourceMappingTableFieldService, KsDataFieldService ksDataFieldService) {
        super(service);
        this.ksDataTypeService = service;
        this.sysSourceMappingTableService = sysSourceMappingTableService;
        this.sysSourceMappingTableFieldService = sysSourceMappingTableFieldService;
        this.ksDataFieldService = ksDataFieldService;
    }

    @GetMapping("listFieldByKsFlowGraphId")
    public CommonResult listFieldByKsFlowGraphId(String ksFlowGraphId,String tableName) {
        return CommonResult.success(ksDataTypeService.listFieldByKsFlowGraphId(ksFlowGraphId,tableName));
    }

    @PostMapping("editPower")
    public CommonResult<KsDataType> editPower(@RequestBody KsDataTypeRequestModel ksDataType) {
        return CommonResult.success(ksDataTypeService.editPower(ksDataType));
    }

    @PostMapping("generateDataTypeField")
    public CommonResult<List<KsDataField>> generateDataTypeField(@RequestBody KsDataTypeRequestModel ksDataType) {
        return CommonResult.success(ksDataTypeService.generateDataTypeField(ksDataType));
    }

    @GetMapping("listColumns")
    public CommonResult<List<KsDataField>> listColumns(String targetTable, String dbCode) {
        return CommonResult.success(ksDataTypeService.listColumns(targetTable, dbCode));
    }

    @PostMapping("exportKsDataType")
    public CommonResult exportKsDataType(@RequestBody List<String> ksDataTypeCodeList, HttpServletResponse response) {
        return CommonResult.success(ksDataTypeService.exportKsDataType(ksDataTypeCodeList, response));
    }


    @PostMapping("importKsDataType")
    public CommonResult importKsDataType(@RequestParam("file") MultipartFile file, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        return CommonResult.success(ksDataTypeService.importKsDataType(file, auth));
    }

    @GetMapping("autoCreateKsDataType")
    public void autoCreateKsDataType(@RequestParam("tableName") String tableName, @RequestParam("dbCode") String dbCode, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        SysSourceMappingTable sysSourceMappingTableParam = new SysSourceMappingTable();
        sysSourceMappingTableParam.setTableName(tableName);
        sysSourceMappingTableParam.setDbCode(dbCode);
        SysSourceMappingTableField sysSourceMappingTableFieldParam = new SysSourceMappingTableField();
        sysSourceMappingTableFieldParam.setTableName(tableName);
        sysSourceMappingTableFieldParam.setDbCode(dbCode);

        Optional<SysSourceMappingTable> sysSourceMappingTable = sysSourceMappingTableService.findByEntity(sysSourceMappingTableParam);
        List<SysSourceMappingTableField> sysSourceMappingTableFields = sysSourceMappingTableFieldService.findAllByEntity(sysSourceMappingTableFieldParam);
        KsDataType ksDataType = new KsDataType();
        if (!sysSourceMappingTable.isPresent()) {
            return;
        }
        ksDataType.setCode(sysSourceMappingTable.get().getTableName());
        if (StringUtils.isNotBlank(sysSourceMappingTable.get().getTableAlias())) {
            ksDataType.setName(sysSourceMappingTable.get().getTableAlias());
        } else {
            ksDataType.setName(sysSourceMappingTable.get().getTableName());
        }
        ksDataTypeService.save(ksDataType, auth);

        List<KsDataField> ksDataFieldList = new ArrayList<>();
        sysSourceMappingTableFields.forEach(sysSourceMappingTableField -> {
            KsDataField ksDataField = new KsDataField();
            ksDataField.setDataTypeCode(sysSourceMappingTable.get().getTableName());
            ksDataField.setKsDataType(ksDataType);
            ksDataField.setCode(sysSourceMappingTableField.getTableField());
            if (StringUtils.isNotBlank(sysSourceMappingTableField.getTableFieldName())) {
                ksDataField.setFieldName(sysSourceMappingTableField.getTableFieldName());
            } else {
                ksDataField.setFieldName(sysSourceMappingTableField.getTableField());
            }
            ksDataField.setFieldType("string");
            ksDataFieldList.add(ksDataField);

            sysSourceMappingTableField.setKsDataFieldCode(sysSourceMappingTableField.getTableField());
        });
        ksDataFieldService.saveAll(ksDataFieldList, auth);
        sysSourceMappingTableFieldService.saveAll(sysSourceMappingTableFields, auth);

    }


    @PostMapping("pageKsDataType")
    public JSONObject pageKsDataType(@RequestBody KsDataTypeRequestModel ksDataType, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        return ksDataTypeService.pageKsDataType(ksDataType, auth);
    }

}
